//
//  ZSWelcomeViewController.swift
//  ZSWeibo
//
//  Created by zsj1992 on 16/4/11.
//  Copyright © 2016年 ichange. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class ZSWelcomeViewController: UIViewController {
    
    
    
    private lazy var imgBgView:UIImageView = {
        let bg = UIImageView(image:UIImage(named: "ad_background"))
        return bg
    }()
    //头像
    private lazy var imgIconView:UIImageView = {
        let icon = UIImageView(image:UIImage(named: "avatar_default_big"))
        icon.cornerRadius = 45
        return icon
    }()
    
    
    private lazy var lblName : UILabel = {
        
        let l = UILabel()
        
        l.text = "sddsd"
        
        l.textColor = UIColor.darkGrayColor()
        
        l.textAlignment = .Center
        
        l.font = UIFont.systemFontOfSize(14)
        
        l.sizeToFit()
        
        return l
        
    }()
    
    
    override func loadView() {
        view = imgBgView
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setUpUI()
        
        let userAccount = ZSUserAccountViewModel.sharedUserViewModel.readUserAccountData()
        
//        print(userAccount?.avatar_large)
        
        imgIconView.sd_setImageWithURL(NSURL(string: (userAccount?.avatar_large)!))
        lblName.text = userAccount?.name
    }
    

    override func viewDidAppear(animated: Bool) {
        
        lblName.alpha = 0
        
        imgIconView.snp_updateConstraints { (make) -> Void in
            
            make.center.equalTo(view.snp_center).offset(CGPointMake(0, -200))

        }

        UIView.animateWithDuration(1.0, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 5, options: [], animations: { () -> Void in
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in

                
                //欢迎界面显示完毕,跳到home页
                
UIView.animateWithDuration(1.0, animations: { () -> Void in
    self.lblName.alpha = 1.0
    
    }, completion: { (_) -> Void in
        
        
        //这样直接切换根控制器不好,需要在appdelegate中利用通知机制来进行根控制器的切换
//        UIApplication.sharedApplication().keyWindow?.rootViewController = ZSTabBarController()

        //这个时候传的最后的那个字符串,是为了在appdelegate中进行判断,切换对应的控制器
        NSNotificationCenter.defaultCenter().postNotificationName(changeRootViewController, object: "tabVC")


})
        }

    }
    
    
    
    
    
    
    func setUpUI(){
        
        view.addSubview(imgIconView)
        view.addSubview(lblName)
        
        //设置约束
        imgIconView.snp_makeConstraints { (make) -> Void in
            
            make.width.equalTo(90)
            
            make.height.equalTo(90)
            
            make.center.equalTo(view.snp_center).offset(CGPointMake(0, 100))
        }
        
        lblName.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(imgIconView.snp_centerX)
            make.top.equalTo(imgIconView.snp_bottom).offset(20)
        }
        
    }

}







